package vsvm.gui.forms;

import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import java.awt.GridBagLayout;
import javax.swing.JComboBox;

import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.Insets;

import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JButton;
import javax.swing.BorderFactory;
import javax.swing.border.TitledBorder;
import java.awt.Font;
import java.awt.Color;
import java.util.ArrayList;

import javax.swing.JList;

import org.jfree.chart.ChartPanel;

import vsvm.data.filter.AbstractAttributeFilter;
import vsvm.data.filter.AbstractFilter;
import vsvm.data.filter.ranking.AbstractRankingFilter;
import vsvm.data.model.AbstractAttribute;
import vsvm.data.model.AbstractDataModel;
import vsvm.graphics.charts.FeatureImportanceHistogram;
import vsvm.gui.Environment;
import javax.swing.JProgressBar;

public class FilterForm extends JFrame {

	private static final long serialVersionUID = 100L;
	private JPanel jContentPane = null;
	private JScrollPane jScrollPaneOptions = null;
	private JPanel jPanelRankOptions = null;
	private JComboBox cmbData = null;
	private JLabel jLabelSupervised = null;
	private JLabel jLabelFisher = null;
	private JLabel jLabelSoap = null;
	private JLabel jLabelUnsupervised = null;
	private JLabel jLabelLaplacian = null;
	private JLabel jLabelEntropy = null;
	private JRadioButton radiobtnFisher = null;
	private JRadioButton radiobtnSoap = null;
	private JRadioButton radiobtnLaplacian = null;
	private JRadioButton radiobtnEntropy = null;
	private JButton btnRank = null;
	private JLabel jLabelDataModels = null;
	private JButton btnTrashhold = null;
	private JButton btnSelect = null;
	private JButton btnClear = null;
	private JButton btnFilter = null;
	private JButton btnUpdate = null;
	private JButton btnCancel = null;
	private JScrollPane jScrollPaneHist = null;
	private JPanel pnlAttributeHistogram = null;
	private JScrollPane jScrollPane = null;
	private JList lstAttributes = null;
	
	private FeatureImportanceHistogram hist = null;
	private double[] values = null;
	private ChartPanel chart = null;
	private String name = null;
	private JButton jButton = null;
	private JPanel jPanel = null;
	private JScrollPane jScrollPane1 = null;
	private JButton jButton1 = null;
	private JButton jButton2 = null;
	private JButton jButton3 = null;
	private JButton jButton4 = null;
	
	private FilterForm me;
	private AbstractFilter f;  //  @jve:decl-index=0:
	public AbstractFilter getF() {
		return f;
	}

	public void setF(AbstractFilter f) {
		this.f = f;
	}

	private AbstractRankingFilter r;  //  @jve:decl-index=0:
	private boolean threadBusy = false;
	private Thread threadFilter = null;

	/**
	 * This method initializes jScrollPaneOptions	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPaneOptions() {
		if (jScrollPaneOptions == null) {
			jScrollPaneOptions = new JScrollPane();
			jScrollPaneOptions.setViewportView(getJPanelRankOptions());
		}
		return jScrollPaneOptions;
	}

	/**
	 * This method initializes jPanelRankOptions	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelRankOptions() {
		if (jPanelRankOptions == null) {
			GridBagConstraints gridBagConstraints61 = new GridBagConstraints();
			gridBagConstraints61.gridx = 2;
			gridBagConstraints61.insets = new Insets(5, 0, 5, 0);
			gridBagConstraints61.gridy = 12;
			GridBagConstraints gridBagConstraints16 = new GridBagConstraints();
			gridBagConstraints16.gridx = 2;
			gridBagConstraints16.insets = new Insets(5, 0, 5, 0);
			gridBagConstraints16.gridy = 13;
			GridBagConstraints gridBagConstraints22 = new GridBagConstraints();
			gridBagConstraints22.gridx = 2;
			gridBagConstraints22.insets = new Insets(5, 0, 5, 0);
			gridBagConstraints22.gridy = 16;
			GridBagConstraints gridBagConstraints14 = new GridBagConstraints();
			gridBagConstraints14.gridx = 2;
			gridBagConstraints14.anchor = GridBagConstraints.EAST;
			gridBagConstraints14.insets = new Insets(25, 0, 5, 0);
			gridBagConstraints14.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints14.gridy = 15;
			GridBagConstraints gridBagConstraints13 = new GridBagConstraints();
			gridBagConstraints13.gridx = 2;
			gridBagConstraints13.insets = new Insets(5, 0, 0, 0);
			gridBagConstraints13.gridy = 11;
			GridBagConstraints gridBagConstraints12 = new GridBagConstraints();
			gridBagConstraints12.gridx = 2;
			gridBagConstraints12.gridheight = 1;
			gridBagConstraints12.fill = GridBagConstraints.NONE;
			gridBagConstraints12.insets = new Insets(5, 0, 0, 0);
			gridBagConstraints12.gridy = 7;
			GridBagConstraints gridBagConstraints171 = new GridBagConstraints();
			gridBagConstraints171.anchor = GridBagConstraints.EAST;
			gridBagConstraints171.insets = new Insets(5, 20, 0, 15);
			gridBagConstraints171.gridx = 1;
			gridBagConstraints171.gridy = 19;
			gridBagConstraints171.ipadx = 0;
			gridBagConstraints171.weightx = 0.0;
			gridBagConstraints171.weighty = 0.0;
			gridBagConstraints171.fill = GridBagConstraints.HORIZONTAL;
			GridBagConstraints gridBagConstraints161 = new GridBagConstraints();
			gridBagConstraints161.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints161.gridx = 1;
			gridBagConstraints161.gridy = 17;
			gridBagConstraints161.insets = new Insets(5, 20, 5, 15);
			GridBagConstraints gridBagConstraints15 = new GridBagConstraints();
			gridBagConstraints15.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints15.gridx = 1;
			gridBagConstraints15.gridy = 18;
			gridBagConstraints15.weightx = 0.0;
			gridBagConstraints15.insets = new Insets(5, 20, 5, 15);
			GridBagConstraints gridBagConstraints141 = new GridBagConstraints();
			gridBagConstraints141.anchor = GridBagConstraints.EAST;
			gridBagConstraints141.insets = new Insets(5, 20, 5, 15);
			gridBagConstraints141.gridx = 1;
			gridBagConstraints141.gridy = 16;
			gridBagConstraints141.ipadx = 6;
			gridBagConstraints141.fill = GridBagConstraints.HORIZONTAL;
			GridBagConstraints gridBagConstraints131 = new GridBagConstraints();
			gridBagConstraints131.anchor = GridBagConstraints.EAST;
			gridBagConstraints131.insets = new Insets(25, 20, 5, 15);
			gridBagConstraints131.gridx = 1;
			gridBagConstraints131.gridy = 15;
			gridBagConstraints131.ipadx = 0;
			gridBagConstraints131.fill = GridBagConstraints.HORIZONTAL;
			GridBagConstraints gridBagConstraints51 = new GridBagConstraints();
			gridBagConstraints51.anchor = GridBagConstraints.EAST;
			gridBagConstraints51.insets = new Insets(5, 20, 5, 15);
			gridBagConstraints51.gridwidth = 1;
			gridBagConstraints51.gridx = 1;
			gridBagConstraints51.gridy = 13;
			gridBagConstraints51.ipadx = 0;
			gridBagConstraints51.fill = GridBagConstraints.HORIZONTAL;
			GridBagConstraints gridBagConstraints21 = new GridBagConstraints();
			gridBagConstraints21.anchor = GridBagConstraints.WEST;
			gridBagConstraints21.insets = new Insets(0, 0, 5, 5);
			gridBagConstraints21.gridx = 1;
			gridBagConstraints21.gridy = 0;
			gridBagConstraints21.fill = GridBagConstraints.HORIZONTAL;
			jLabelDataModels = new JLabel();
			jLabelDataModels.setText("Data Models");
			GridBagConstraints gridBagConstraints10 = new GridBagConstraints();
			gridBagConstraints10.anchor = GridBagConstraints.EAST;
			gridBagConstraints10.insets = new Insets(5, 20, 5, 15);
			gridBagConstraints10.gridheight = 1;
			gridBagConstraints10.gridwidth = 1;
			gridBagConstraints10.gridx = 1;
			gridBagConstraints10.gridy = 12;
			gridBagConstraints10.ipadx = 2;
			gridBagConstraints10.ipady = 0;
			gridBagConstraints10.weightx = 0.0;
			gridBagConstraints10.fill = GridBagConstraints.HORIZONTAL;
			GridBagConstraints gridBagConstraints9 = new GridBagConstraints();
			gridBagConstraints9.anchor = GridBagConstraints.WEST;
			gridBagConstraints9.insets = new Insets(0, 5, 0, 0);
			gridBagConstraints9.gridx = 0;
			gridBagConstraints9.gridy = 11;
			gridBagConstraints9.fill = GridBagConstraints.NONE;
			GridBagConstraints gridBagConstraints8 = new GridBagConstraints();
			gridBagConstraints8.anchor = GridBagConstraints.WEST;
			gridBagConstraints8.gridx = 0;
			gridBagConstraints8.gridy = 7;
			gridBagConstraints8.insets = new Insets(0, 5, 0, 0);
			GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
			gridBagConstraints7.anchor = GridBagConstraints.WEST;
			gridBagConstraints7.gridx = 0;
			gridBagConstraints7.gridy = 4;
			gridBagConstraints7.insets = new Insets(0, 5, 0, 0);
			GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
			gridBagConstraints6.anchor = GridBagConstraints.WEST;
			gridBagConstraints6.insets = new Insets(0, 5, 0, 0);
			gridBagConstraints6.gridwidth = 2;
			gridBagConstraints6.gridx = 0;
			gridBagConstraints6.gridy = 3;
			gridBagConstraints6.ipadx = 0;
			gridBagConstraints6.fill = GridBagConstraints.NONE;
			GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
			gridBagConstraints5.anchor = GridBagConstraints.WEST;
			gridBagConstraints5.insets = new Insets(5, 10, 5, 5);
			gridBagConstraints5.gridx = 1;
			gridBagConstraints5.gridy = 11;
			gridBagConstraints5.fill = GridBagConstraints.HORIZONTAL;
			jLabelEntropy = new JLabel();
			jLabelEntropy.setText("Entropy Measure");
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.anchor = GridBagConstraints.WEST;
			gridBagConstraints3.insets = new Insets(5, 10, 5, 5);
			gridBagConstraints3.gridx = 1;
			gridBagConstraints3.gridy = 7;
			gridBagConstraints3.fill = GridBagConstraints.HORIZONTAL;
			jLabelLaplacian = new JLabel();
			jLabelLaplacian.setText("Laplacian Score");
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			gridBagConstraints4.anchor = GridBagConstraints.WEST;
			gridBagConstraints4.insets = new Insets(8, 10, 0, 5);
			gridBagConstraints4.gridx = 1;
			gridBagConstraints4.gridy = 6;
			gridBagConstraints4.fill = GridBagConstraints.HORIZONTAL;
			jLabelUnsupervised = new JLabel();
			jLabelUnsupervised.setText("Unsupervised ranking:");
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.anchor = GridBagConstraints.WEST;
			gridBagConstraints2.insets = new Insets(0, 10, 8, 5);
			gridBagConstraints2.gridx = 1;
			gridBagConstraints2.gridy = 4;
			gridBagConstraints2.fill = GridBagConstraints.HORIZONTAL;
			jLabelSoap = new JLabel();
			jLabelSoap.setText("Soap");
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.anchor = GridBagConstraints.WEST;
			gridBagConstraints1.insets = new Insets(5, 10, 0, 5);
			gridBagConstraints1.gridx = 1;
			gridBagConstraints1.gridy = 3;
			gridBagConstraints1.fill = GridBagConstraints.HORIZONTAL;
			jLabelFisher = new JLabel();
			jLabelFisher.setText("Fisher Score");
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.anchor = GridBagConstraints.WEST;
			gridBagConstraints.insets = new Insets(25, 10, 5, 5);
			gridBagConstraints.gridx = 1;
			gridBagConstraints.gridy = 2;
			gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
			jLabelSupervised = new JLabel();
			jLabelSupervised.setText("Supervised ranking:");
			GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
			gridBagConstraints11.fill = GridBagConstraints.BOTH;
			gridBagConstraints11.gridx = 1;
			gridBagConstraints11.gridy = 1;
			gridBagConstraints11.weightx = 1.0;
			gridBagConstraints11.insets = new Insets(5, 0, 15, 5);
			jPanelRankOptions = new JPanel();
			jPanelRankOptions.setLayout(new GridBagLayout());
			jPanelRankOptions.setBorder(BorderFactory.createTitledBorder(null, "Feature Importance", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font("Dialog", Font.BOLD, 12), new Color(51, 51, 51)));
			jPanelRankOptions.add(getCmbData(), gridBagConstraints11);
			jPanelRankOptions.add(jLabelSupervised, gridBagConstraints);
			jPanelRankOptions.add(jLabelFisher, gridBagConstraints1);
			jPanelRankOptions.add(jLabelSoap, gridBagConstraints2);
			jPanelRankOptions.add(jLabelUnsupervised, gridBagConstraints4);
			jPanelRankOptions.add(jLabelLaplacian, gridBagConstraints3);
			jPanelRankOptions.add(jLabelEntropy, gridBagConstraints5);
			jPanelRankOptions.add(getRadiobtnFisher(), gridBagConstraints6);
			jPanelRankOptions.add(getRadiobtnSoap(), gridBagConstraints7);
			jPanelRankOptions.add(getRadiobtnLaplacian(), gridBagConstraints8);
			jPanelRankOptions.add(getRadiobtnEntropy(), gridBagConstraints9);
			jPanelRankOptions.add(getBtnRank(), gridBagConstraints10);
			jPanelRankOptions.add(jLabelDataModels, gridBagConstraints21);
			jPanelRankOptions.add(getBtnTrashhold(), gridBagConstraints51);
			jPanelRankOptions.add(getBtnSelect(), gridBagConstraints131);
			jPanelRankOptions.add(getBtnClear(), gridBagConstraints141);
			jPanelRankOptions.add(getBtnFilter(), gridBagConstraints15);
			jPanelRankOptions.add(getBtnUpdate(), gridBagConstraints161);
			jPanelRankOptions.add(getBtnCancel(), gridBagConstraints171);
			jPanelRankOptions.add(getJButton(), gridBagConstraints12);
			jPanelRankOptions.add(getJButton1(), gridBagConstraints13);
			jPanelRankOptions.add(getJButton2(), gridBagConstraints14);
			jPanelRankOptions.add(getJButton3(), gridBagConstraints22);
			jPanelRankOptions.add(getJButton4(), gridBagConstraints16);
			jPanelRankOptions.add(getBtnRankOptions(), gridBagConstraints61);
		}
		return jPanelRankOptions;
	}

	/**
	 * This method initializes cmbData	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getCmbData() {
		if (cmbData == null) {
			cmbData = new JComboBox();
			cmbData.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if (cmbData.getSelectedItem() != null)
						loadAttributes((AbstractDataModel)cmbData.getSelectedItem());
				}
			});
		}
		return cmbData;
	}

	/**
	 * This method initializes radiobtnFisher	
	 * 	
	 * @return javax.swing.JRadioButton	
	 */
	private JRadioButton getRadiobtnFisher() {
		if (radiobtnFisher == null) {
			radiobtnFisher = new JRadioButton();
			radiobtnFisher.setSelected(true);
			radiobtnFisher.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					radiobtnFisher.setSelected(true);
					radiobtnEntropy.setSelected(false);
					radiobtnLaplacian.setSelected(false);
					radiobtnSoap.setSelected(false);
					jButton.setEnabled(false);
					jButton1.setEnabled(false);
				}
			});
		}
		return radiobtnFisher;
	}

	/**
	 * This method initializes radiobtnSoap	
	 * 	
	 * @return javax.swing.JRadioButton	
	 */
	private JRadioButton getRadiobtnSoap() {
		if (radiobtnSoap == null) {
			radiobtnSoap = new JRadioButton();
			radiobtnSoap.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					radiobtnFisher.setSelected(false);
					radiobtnEntropy.setSelected(false);
					radiobtnLaplacian.setSelected(false);
					radiobtnSoap.setSelected(true);
					jButton.setEnabled(false);
					jButton1.setEnabled(false);
				}
			});
		}
		return radiobtnSoap;
	}

	/**
	 * This method initializes radiobtnLaplacian	
	 * 	
	 * @return javax.swing.JRadioButton	
	 */
	private JRadioButton getRadiobtnLaplacian() {
		if (radiobtnLaplacian == null) {
			radiobtnLaplacian = new JRadioButton();
			radiobtnLaplacian.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					radiobtnFisher.setSelected(false);
					radiobtnEntropy.setSelected(false);
					radiobtnLaplacian.setSelected(true);
					radiobtnSoap.setSelected(false);
					jButton.setEnabled(true);
					jButton1.setEnabled(false);
				}
			});
		}
		return radiobtnLaplacian;
	}

	/**
	 * This method initializes radiobtnEntropy	
	 * 	
	 * @return javax.swing.JRadioButton	
	 */
	private JRadioButton getRadiobtnEntropy() {
		if (radiobtnEntropy == null) {
			radiobtnEntropy = new JRadioButton();
			radiobtnEntropy.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					radiobtnFisher.setSelected(false);
					radiobtnEntropy.setSelected(true);
					radiobtnLaplacian.setSelected(false);
					radiobtnSoap.setSelected(false);
					jButton.setEnabled(false);
					jButton1.setEnabled(true);
				}
			});
		}
		return radiobtnEntropy;
	}

	/**
	 * This method initializes btnRank	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnRank() {
		if (btnRank == null) {
			btnRank = new JButton();
			btnRank.setText("Rank");
			btnRank.addActionListener(new java.awt.event.ActionListener() 
			{
				public void actionPerformed(java.awt.event.ActionEvent e)
				{
					if(threadBusy)
					{
						JOptionPane.showMessageDialog(me, "Reanking features! " +
								"Press Cancel to stop.", "Thread is busy!", 0);
						return;
					}
					
					if (cmbData.getSelectedItem() != null) 
					{
						if(chart != null)
						{
							pnlAttributeHistogram.remove(chart);
						}
						
						if(radiobtnFisher.isSelected())
						{
							r = ((AbstractAttributeFilter)f).getFisherScore();
							name = "Fisher Score";
						} 
						else if(radiobtnSoap.isSelected())
						{
							r = ((AbstractAttributeFilter)f).getSoap();
							name = "Soap";
						} 
						else if(radiobtnLaplacian.isSelected())
						{
							r = ((AbstractAttributeFilter)f).getLaplacianScore();
							name = "Laplacian Score";
						} 
						else if(radiobtnEntropy.isSelected())
						{
							r = ((AbstractAttributeFilter)f).getEntropyMeasure();
							name = "Entropy Measure";
						}
						
						r.setDataModel((AbstractDataModel)cmbData.getSelectedItem());
						jProgressBar.setIndeterminate(true);
						
						threadFilter = new ThreadFilter();
						threadFilter.start();
					}
				}
			});
		}
		return btnRank;
	}

	/**
	 * This method initializes btnTrashhold	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnTrashhold() {
		if (btnTrashhold == null) {
			btnTrashhold = new JButton();
			btnTrashhold.setText("Threshold");
			btnTrashhold.addActionListener(new java.awt.event.ActionListener() 
			{
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					if(threadBusy)
					{
						JOptionPane.showMessageDialog(me, "Reanking features! " +
								"Press Cancel to stop.", "Thread is busy!", 0);
						return;
					}
					
					if(chart != null)
					{
						int[] sel = new int[lstAttributes.getModel().getSize()];	
						double[] val = new double[values.length];
						int classIndex = ((AbstractDataModel)cmbData.getSelectedItem()).getClassAttributeIndex();
						((AbstractAttributeFilter) f).getThresh().afterThreshold(values, classIndex, sel, val);
	
						lstAttributes.setSelectedIndices(sel);		
						pnlAttributeHistogram.remove(chart);
						hist = new FeatureImportanceHistogram(val, name, lstAttributes);
						chart = hist.getChartPanel();
						pnlAttributeHistogram.add(chart, BorderLayout.CENTER);
						pnlAttributeHistogram.updateUI();
					}
				}
			});
		}
		return btnTrashhold;
	}

	/**
	 * This method initializes btnSelect	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnSelect() {
		if (btnSelect == null) {
			btnSelect = new JButton();
			btnSelect.setText("Save selected");
			btnSelect.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					if(threadBusy)
					{
						JOptionPane.showMessageDialog(me, "Reanking features! " +
								"Press Cancel to stop.", "Thread is busy!", 0);
						return;
					}
					
					if (cmbData.getSelectedItem() != null) 
					{												
						Object[] arr = lstAttributes.getSelectedValues();
						((AbstractAttributeFilter) f).getSave().saveSelected(arr);
					} 
				}
			});
		}
		return btnSelect;
	}

	/**
	 * This method initializes btnClear	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnClear() {
		if (btnClear == null) {
			btnClear = new JButton();
			btnClear.setText("Select saved");
			btnClear.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					if(threadBusy)
					{
						JOptionPane.showMessageDialog(me, "Reanking features! " +
								"Press Cancel to stop.", "Thread is busy!", 0);
						return;
					}
					if (cmbData.getSelectedItem() != null) 
					{
						int[] sel = ((AbstractAttributeFilter) f).getSelect().selectSaved(lstAttributes);											
						lstAttributes.setSelectedIndices(sel);	
					}
				}
			});
		}
		return btnClear;
	}

	/**
	 * This method initializes btnFilter	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnFilter() {
		if (btnFilter == null) {
			btnFilter = new JButton();
			btnFilter.setText("Filter");
			btnFilter.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					if(threadBusy)
					{
						JOptionPane.showMessageDialog(me, "Reanking features! " +
								"Press Cancel to stop.", "Thread is busy!", 0);
						return;
					}
					if (cmbData.getSelectedItem() != null) 
					{
						f.setDataModel((AbstractDataModel)cmbData.getSelectedItem());
						ArrayList<AbstractAttribute> attr = new ArrayList<AbstractAttribute>();
						Object[] arr = lstAttributes.getSelectedValues();
						for (int i = 0; i < arr.length; ++i)
						{
							attr.add((AbstractAttribute)arr[i]);
						}
						((AbstractAttributeFilter)f).setAttributes(attr);
						AbstractDataModel dm = f.filter();
						dm.setName(JOptionPane.showInputDialog("Data model name", "filtered"));
						Environment.getEnvironment().getWorkspace().addDataModel(dm);
						
						dispose();
					}
				}
			});
		}
		return btnFilter;
	}

	/**
	 * This method initializes btnUpdate	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnUpdate() {
		if (btnUpdate == null) {
			btnUpdate = new JButton();
			btnUpdate.setText("Update");
			btnUpdate.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					if(threadBusy)
					{
						JOptionPane.showMessageDialog(me, "Reanking features! " +
								"Press Cancel to stop.", "Thread is busy!", 0);
						return;
					}
					if (cmbData.getSelectedItem() != null) 
					{
						f.setDataModel((AbstractDataModel)cmbData.getSelectedItem());
						ArrayList<AbstractAttribute> attr = new ArrayList<AbstractAttribute>();
						Object[] arr = lstAttributes.getSelectedValues();
						for (int i = 0; i < arr.length; ++i)
							attr.add((AbstractAttribute)arr[i]);
						((AbstractAttributeFilter)f).setAttributes(attr);
						f.update();
						
						dispose();
					}
				}
			});
		}
		return btnUpdate;
	}

	/**
	 * This method initializes btnCancel	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnCancel() {
		if (btnCancel == null) {
			btnCancel = new JButton();
			btnCancel.setText("Cancel");
			btnCancel.addActionListener(new java.awt.event.ActionListener() {
				@SuppressWarnings("deprecation")
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{	
					if(threadBusy && threadFilter != null)
					{
						threadFilter.stop();	
					}
					dispose();
				}
			});
		}
		return btnCancel;
	}

	/**
	 * This method initializes jScrollPaneHist	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPaneHist() {
		if (jScrollPaneHist == null) {
			jScrollPaneHist = new JScrollPane();
			jScrollPaneHist.setViewportView(getJPanel());
		}
		return jScrollPaneHist;
	}

	/**
	 * This method initializes pnlAttributeHistogram	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	public JPanel getPnlAttributeHistogram() {
		if (pnlAttributeHistogram == null) {
			pnlAttributeHistogram = new JPanel();
			pnlAttributeHistogram.setLayout(new BorderLayout());
			pnlAttributeHistogram.setBorder(BorderFactory.createTitledBorder(null, "Histogram", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font("Dialog", Font.BOLD, 12), new Color(51, 51, 51)));
		}
		return pnlAttributeHistogram;
	}

	/**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setViewportView(getLstAttributes());
		}
		return jScrollPane;
	}

	/**
	 * This method initializes lstAttributes	
	 * 	
	 * @return javax.swing.JList	
	 */
	public JList getLstAttributes() {
		if (lstAttributes == null) {
			lstAttributes = new JList();
		}
		return lstAttributes;
	}

	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setText("...");
			jButton.setEnabled(false);
			jButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					new LaplacianForm(me, ((AbstractAttributeFilter)f).getLaplacianScore());
				}
			});
		}
		return jButton;
	}

	/**
	 * @param args
	 */

	public FilterForm(Frame parent, AbstractFilter filter) {
		super();
		initialize();
		setVisible(true);
		setLocationRelativeTo(parent);
		this.me = this;;
		f = filter;
		loadData();
	}


	private void loadData() {
		ArrayList<AbstractDataModel> dm = Environment.getEnvironment().getWorkspace().getDataModels();
		DefaultComboBoxModel m1 = new DefaultComboBoxModel(dm.toArray());
		cmbData.setModel(m1);
		if (dm.size() > 0)
			cmbData.setSelectedIndex(0);
	}
	
	private void loadAttributes(AbstractDataModel dm) {
		DefaultListModel m = new DefaultListModel();
		for (int i = 0; i < dm.getAttributeCount(); ++i)
			m.addElement(dm.getAttribute(i));
		
		lstAttributes.setModel(m);
	}
	
	public void setDataModel(AbstractDataModel dm) {
		cmbData.setSelectedItem(dm);
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(850, 572);

		this.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		this.setContentPane(getJContentPane());
		this.setTitle("Filtering");
		this.addWindowListener(new java.awt.event.WindowAdapter() {
			@SuppressWarnings("deprecation")
			public void windowClosing(java.awt.event.WindowEvent e) {
				if(threadBusy && threadFilter != null)
				{
					threadFilter.stop();	
				}
			}
		});
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getJScrollPaneOptions(), BorderLayout.WEST);
			jContentPane.add(getJScrollPaneHist(), BorderLayout.CENTER);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			jPanel = new JPanel();
			jPanel.setLayout(new BorderLayout());
			jPanel.add(getJPanel1(), BorderLayout.SOUTH);
			jPanel.add(getJScrollPane1(), BorderLayout.CENTER);
		}
		return jPanel;
	}

	/**
	 * This method initializes jScrollPane1	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane1() {
		if (jScrollPane1 == null) {
			jScrollPane1 = new JScrollPane();
			jScrollPane1.setViewportView(getPnlAttributeHistogram());
		}
		return jScrollPane1;
	}

	/**
	 * This method initializes jButton1	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton1() {
		if (jButton1 == null) {
			jButton1 = new JButton();
			jButton1.setText("...");
			jButton1.setEnabled(false);
			jButton1.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					new EntropyMeasureForm(me, ((AbstractAttributeFilter)f).getEntropyMeasure());
				}
			});
		}
		return jButton1;
	}

	/**
	 * This method initializes jButton2	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton2() {
		if (jButton2 == null) {
			jButton2 = new JButton();
			jButton2.setText("...");
			jButton2.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					if(threadBusy)
					{
						JOptionPane.showMessageDialog(me, "Reanking features! " +
								"Press Cancel to stop.", "Thread is busy!", 0);
						return;
					}
					new SaveSelectedForm(me, ((AbstractAttributeFilter) f).getSave());	
				}
			});
		}
		return jButton2;
	}

	/**
	 * This method initializes jButton3	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton3() {
		if (jButton3 == null) {
			jButton3 = new JButton();
			jButton3.setText("...");
			jButton3.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					if(threadBusy)
					{
						JOptionPane.showMessageDialog(me, "Reanking features! " +
								"Press Cancel to stop.", "Thread is busy!", 0);
						return;
					}
					new SelectSavedForm(me, ((AbstractAttributeFilter) f).getSelect());	
				}
			});
		}
		return jButton3;
	}

	/**
	 * This method initializes jButton4	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton4() {
		if (jButton4 == null) {
			jButton4 = new JButton();
			jButton4.setText("...");
			jButton4.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					if(threadBusy)
					{
						JOptionPane.showMessageDialog(me, "Reanking features! " +
								"Press Cancel to stop.", "Thread is busy!", 0);
						return;
					}

					new ThresholdForm(me, ((AbstractAttributeFilter) f).getThresh());
				}
			});
		}
		return jButton4;
	}
	
	private JPanel jPanel1 = null;
	private JPanel jPanel2 = null;
	private JProgressBar jProgressBar = null;
	private JButton btnRankOptions = null;

	public boolean isThreadBusy() {
		return threadBusy;
	}

	public void setThreadBusy(boolean threadBusy) {
		this.threadBusy = threadBusy;
	}

	public FeatureImportanceHistogram getHist() {
		return hist;
	}

	public void setHist(FeatureImportanceHistogram hist) {
		this.hist = hist;
	}

	public void setPnlAttributeHistogram(JPanel pnlAttributeHistogram) {
		this.pnlAttributeHistogram = pnlAttributeHistogram;
	}

	public AbstractRankingFilter getR() {
		return r;
	}

	public void setR(AbstractRankingFilter r) {
		this.r = r;
	}

	public ChartPanel getChart() {
		return chart;
	}

	public void setChart(ChartPanel chart) {
		this.chart = chart;
	}

	public void setLstAttributes(JList lstAttributes) {
		this.lstAttributes = lstAttributes;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public double[] getValues() {
		return values;
	}

	public void setValues(double[] values) {
		this.values = values;
	}

	/**
	 * This method initializes jPanel1	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel1() {
		if (jPanel1 == null) {
			jPanel1 = new JPanel();
			jPanel1.setLayout(new BorderLayout());
			jPanel1.add(getJPanel2(), BorderLayout.EAST);
			jPanel1.add(getJScrollPane(), BorderLayout.CENTER);
		}
		return jPanel1;
	}

	/**
	 * This method initializes jPanel2	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel2() {
		if (jPanel2 == null) {
			GridBagConstraints gridBagConstraints17 = new GridBagConstraints();
			gridBagConstraints17.gridx = 0;
			gridBagConstraints17.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints17.insets = new Insets(90, 10, 10, 10);
			gridBagConstraints17.gridwidth = 2;
			gridBagConstraints17.gridheight = 2;
			gridBagConstraints17.gridy = 0;
			jPanel2 = new JPanel();
			jPanel2.setLayout(new GridBagLayout());
			jPanel2.add(getJProgressBar(), gridBagConstraints17);
		}
		return jPanel2;
	}

	/**
	 * This method initializes jProgressBar	
	 * 	
	 * @return javax.swing.JProgressBar	
	 */
	public JProgressBar getJProgressBar() {
		if (jProgressBar == null) {
			jProgressBar = new JProgressBar();
		}
		return jProgressBar;
	}

	/**
	 * This method initializes btnRankOptions	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnRankOptions() {
		if (btnRankOptions == null) {
			btnRankOptions = new JButton();
			btnRankOptions.setText("...");
			btnRankOptions.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					if(threadBusy)
					{
						JOptionPane.showMessageDialog(me, "Reanking features! " +
								"Press Cancel to stop.", "Thread is busy!", 0);
						return;
					}

					new NormalizationForm(me, ((AbstractAttributeFilter) f).getNormalization());
				}
			});
		}
		return btnRankOptions;
	}
	

	private class ThreadFilter extends Thread {

	    public void run() 
	    {
	    	try {
				threadBusy = true;	
				jProgressBar.setIndeterminate(true);
				
				values = r.score();
				if(((AbstractAttributeFilter)f).getNormalization().isNormalize())
				{
					values = ((AbstractAttributeFilter)f).getNormalization().normalize(values);
				}		
				me.setHist(new FeatureImportanceHistogram(me.getValues(),
												me.getName(), me.getLstAttributes()));
				me.setChart(me.getHist().getChartPanel());
				me.getPnlAttributeHistogram().add(me.getChart(), BorderLayout.CENTER);
				me.getPnlAttributeHistogram().updateUI();
				
				jProgressBar.setIndeterminate(false);
				threadBusy = false;
			} catch (OutOfMemoryError e) 
			{
				jProgressBar.setIndeterminate(false);
				threadBusy = false;
				
				JOptionPane.showMessageDialog(me, "There is not enough memory for " +
						"the desired calculations!", "Out of Memory", JOptionPane.ERROR_MESSAGE);
			}
	    }

	}

}  //  @jve:decl-index=0:visual-constraint="10,10"


